<template>
  <div>
    <div style="margin: 10px 0">
      <el-input
        placeholder="请输入内容"
        style="width: 200px"
        v-model="courseName"
      ></el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        style="margin: 10px"
        @click="findname"
        >搜索</el-button
      >
      <el-button
        type="info"
        icon="el-icon-remove-outline"
        style="margin: 10px"
        @click="reset"
        >重置</el-button
      >
    </div >
    <div style="margin: 10px 0; " >
      <el-button type="primary" icon="el-icon-info" 
        >学生课程选择</el-button
      >
      
    </div>
    <el-table :data="tableData">
      <el-table-column prop="id" label="id" width="140"> </el-table-column>
      <el-table-column prop="courseName" label="课程设计名" width="120" align="center">
      </el-table-column>
      <el-table-column prop="maxNum" label="最大选择人数" align="center"> </el-table-column>
       <el-table-column prop="selectNum" label="已选人数" align="center"> </el-table-column>
      <el-table-column prop="level" label="作业难度" align="center"> </el-table-column>
    
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="warning" @click="select(scope.row)">确认</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">

      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5,8,20,30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

  </div>
</template>
<script>
import request from "@/utils/request";
import { serverIp } from '@/utils/config';
export default {
  name: "user",

  data() {
    return {
      courseName:'',
      options:[],
      tableData: [],
      form: {},
      username: JSON.parse(localStorage.getItem("user")).username,
      pageSize: 8,
      pageNum: 1,
      total: null,
      dialogFormVisible: false,
    };
  },
  methods: {
    load() {
      this.request
        .get("/course/page", {
          params: {
            courseName:this.courseName,
            pageNum: this.pageNum,
            pageSize: this.pageSize,
          },
        })
        .then((res) => {
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.load();
    },
    findname() {
      this.load();
    },
    select(row) {
      this.form = row;
       request.get("/course/select", {
          params: {
            userName: this.username,
            courseName:row.courseName,
            maxNum:row.maxNum,
            selectNum:row.selectNum
          }
          }).then(res=>{
          if(res.status==200){
              this.$message.success("选课成功")
              this.load();
          }
          else{
              this.$message.error(res.msg)
          }
      }) 
    },
    reset() {
      this.courseName = "";
      this.load()
    },
   
    importSuccess(){
      this.$message.success("传入成功")
      this.load()
    },
   
  },
  created() {
    //请求分页数据
    this.load();
   
  },
};
</script>
<style scoped>
.up{
  margin-left: 10px;
}
.block{
    margin-top: 20px;
    display: flex;
    justify-content: center;
}
</style>